package com.lidong.lowcode.database.constants.database;

import com.lidong.lowcode.database.constants.BaseConst;
import com.lidong.lowcode.database.entity.MySqlColumnTypeLength;

import java.util.HashMap;
import java.util.Locale;
import java.util.Map;

/**
 * Mysql 列类型和长度枚举类
 *
 * @author LiDong
 * @version 1.0.0
 * @createTime 9/6/2022 9:16 PM
 */
public enum SqlColumnEnum {

    /**
     * 默认
     */
    DEFAULT(null, null, null),

    /**
     * int
     */
    INT(1, 1, null),

    /**
     * varchar
     */
    VARCHAR(1, 255, null),

    /**
     * binary
     */
    BINARY(1, 1, null),

    /**
     * char
     */
    CHAR(1, 255, null),

    /**
     * bigInt
     */
    BIGINT(1, 20, null),

    /**
     * decimal
     */
    DECIMAL(2, 10, 2),

    /**
     * double
     */
    DOUBLE(2, 10, 0),

    /**
     * text
     */
    TEXT(0, null, null),

    /**
     * dataTime
     */
    DATETIME(0, null, null),

    /**
     * timestamp
     */
    TIMESTAMP(0, null, null),

    /**
     * float
     */
    FLOAT(2, 10, 0),
    TINYINT(1, 4, null),
    SMALLINT(1, 6, null),
    MEDIUMINT(1, 9, null);

    /**
     * Mysql 字段长度、类型 Map
     */
    public static Map<String, MySqlColumnTypeLength> mySqlColumnTypeLengthMap;

    static {
        mySqlColumnTypeLengthMap = new HashMap<>(BaseConst.MAP_INITIAL_SIZE);
        for (SqlColumnEnum sqlColumnEnum : values()) {
            String typeName = sqlColumnEnum.toString().toLowerCase(Locale.ROOT);
            MySqlColumnTypeLength mySqlColumnTypeLength = new MySqlColumnTypeLength(sqlColumnEnum.length, sqlColumnEnum.defaultLength, sqlColumnEnum.decimalPoint, typeName);
            mySqlColumnTypeLengthMap.put(typeName, mySqlColumnTypeLength);
        }
    }

    /**
     * 长度
     */
    private final Integer length;

    /**
     * 默认长度
     */
    private final Integer defaultLength;

    /**
     * 小数点位数
     */
    private final Integer decimalPoint;

    SqlColumnEnum(Integer length, Integer defaultLength, Integer decimalPoint) {
        this.length = length;
        this.defaultLength = defaultLength;
        this.decimalPoint = decimalPoint;
    }

    public Integer getLength() {
        return length;
    }

    public Integer getDefaultLength() {
        return defaultLength;
    }

    public Integer getDecimalPoint() {
        return decimalPoint;
    }

}
